package com.lesshassles.controller;

import java.util.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import com.lesshassles.model.*;
import com.lesshassles.util.*;

/**
 * Controller for the User List screen.
 * @author David Santoro 
 */
public class TaskListController implements Controller
{
    private TaskBroker taskBroker;
    private ApplicationSecurityManager applicationSecurityManager;
    private String successView;
	public static final String TO_DO_MAP_KEY = "toDoList";
	public static final String TO_BE_DONE_MAP_KEY = "toBeDoneList";

    /**
     * Returns a list of User database objects in ModelAndView.
     * @see com.lesshassles.model.User
     */
    public ModelAndView handleRequest(
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        
    	User currentUser = applicationSecurityManager.getUser(request);
    	int currentUserId = currentUser.getId();
    	
    	List<Task> toDoList = taskBroker.getTasksAssignedToUser(currentUserId);
    	
    	List<Task> toBeDoneList = taskBroker.getTasksOpenedByUser(currentUserId);
    	
    	ModelAndView modelAndView = new ModelAndView(getSuccessView());
    	modelAndView.addObject(TO_DO_MAP_KEY, toDoList);
    	modelAndView.addObject(TO_BE_DONE_MAP_KEY, toBeDoneList);
    	
        return modelAndView;
    }

    /* Getters and Setters */
    public TaskBroker getTaskBroker() {
        return taskBroker;
    }
    public void setTaskBroker(TaskBroker taskBroker) {
        this.taskBroker = taskBroker;
    }

    public String getSuccessView() {
        return successView;
    }
    public void setSuccessView(String successView) {
        this.successView = successView;
    }

	public ApplicationSecurityManager getApplicationSecurityManager() {
		return applicationSecurityManager;
	}
	public void setApplicationSecurityManager(
			ApplicationSecurityManager applicationSecurityManager) {
		this.applicationSecurityManager = applicationSecurityManager;
	}
}
